package com.foreverwin.jzymes.report.sfc.controller;

import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.cutter.domain.ToolBreakLog;
import com.foreverwin.jzymes.production.cutter.service.IExportExcelService;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.production.sfc.mapper.SFCMapper;
import com.foreverwin.jzymes.report.sfc.service.ISfcReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report/sfc")
public class SFCReportController extends BaseController {
    @Autowired
    ISfcReportService sfcReportService;
    @Autowired
    IExportExcelService exportExcelService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    SFCMapper sfcMapper;
    /**
     * 工令报表
     *
     * @param jsonMap
     * @return
     */
    @RequestMapping("/sfcReport")
    public AjaxResult sfcReport(@RequestParam Map<String,Object> jsonMap ){
        jsonMap.put( "site" , CommonMethods.getSite() );
        try{
            List<Map<String,Object>> sfcList = sfcReportService.searchSFCReport( jsonMap );
            return AjaxResult.returnSuccess().put( "value", sfcList );
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }

    /**
     * 导出excel
     * @param paramJson
     * @param response
     * @param request
     */
    @RequestMapping("/doexport")
    public void doexport( String  paramJson , HttpServletResponse response, HttpServletRequest request ){

        try{
            List< Map< String,Object > > list = ObjectMapperUtil.readValue( paramJson, ArrayList.class );

            String[] rowName = new String[]{"序号","工令","状态","机种","制程","料号","物料描述","计划数量","工令数量","入库总数","未入库数量","开始日期","创建日期","创建人员"};
            String title = "工令信息";
            String excel = "SFC.xls";
            List<Object[]> dataList = new ArrayList<Object[]>();
            Object[] objs = null;
            for( int i = 0; i < list.size(); i++ ){
                SFC sfc = sfcMapper.selectSFCById( (String) list.get(i).get( "handle" ) );
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
                SimpleDateFormat simpleDate = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
                sfcReportService.change( sfc , null );
                Map<String,Object> item=itemAdviceService.existItem( sfc.getItemBo() );

                objs = new Object[rowName.length];
                objs[0] = i;
                objs[1] = sfc.getSfc();
                objs[2] = sfc.getStatusBo();
                objs[3] = sfc.getMachineType();
                objs[4] = sfc.getProcedureBo();
                objs[5] = sfc.getItemBo().split(",")[1];
                objs[6] = item.get("DESCRIPTION");
                objs[7] = sfc.getPlanQty();
                objs[8] = sfc.getSfcQty();
                objs[9] = list.get(i).get( "total" );
                objs[10] = list.get(i).get( "differ" );
                objs[11] = simpleDateFormat.format( sfc.getPlanStartTime() );
                objs[12] = simpleDate.format( sfc.getCreatedDateTime() );
                objs[13] = sfc.getCreateUserBo().split(",")[1];

                dataList.add(objs);
            }

            File file = exportExcelService.Export( dataList , title ,rowName ,excel );

            fileDownload( file,true,response,request );

        }catch (Exception e ) {
            throw  BusinessException.build(e.getMessage());
        }
    }

}
